<?php

namespace App\Http\Controllers\Rent;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class AdminController extends Controller
{
    public function login(Request $request){
        ini_set('session.cookie_path', '/');
        session_start();
        if($request->isMethod('post')){
            $username = $_POST['username'];
            $password = $_POST['password'];
            $info = (array)DB::table('admin')->where('username',$username)->first();
            if(!empty($info)){
                if($info['username'] == $username && $info['password']== $password){
                    //登录后写入session
                    $_SESSION['userInfo'] = array('username'=>$username);
                    $data = array(
                        'username'=>$info['username'],
                        'ip'=>getIp(),
                        'create_time'=>time()
                    );
                    DB::table('login_log')->insert($data);
                    $url = url('admin/index.html');
                    echo "<script>alert('登陆成功！');window.location.href = '".$url."';</script>";
                    exit;
                }else{
                    $url = url('admin/login.html');
                    echo "<script>alert('密码错误，请确认后再尝试登陆！');window.location.href = '".$url."';</script>";
                    exit;
                }
            }else{
                $url = url('admin/login.html');
                echo "<script>alert('用户不存在！');window.location.href = '".$url."';</script>";
                exit;
            }
        }else{
            unset($_SESSION['userInfo']);
            return view('admin/login');
        }

    }

    public function index(Request $request){
        $login_log = DB::table('login_log')->take(5)->orderby('create_time','desc')->get()->map(function($v){return (array)$v;})->toArray();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        //今日注册总数
        $todayCount = DB::table('members')->where('create_time','>',$beginToday)->where('create_time','<',$endToday)->count();
        $nanTodayCount = DB::table('members')->where('sex',1)->where('create_time','>',$beginToday)->where('create_time','<',$endToday)->count();
        $nvTodayCount = DB::table('members')->where('sex',2)->where('create_time','>',$beginToday)->where('create_time','<',$endToday)->count();
        //昨日注册总数
        $yesterdayCount = DB::table('members')->where('create_time','>',$beginYesterday)->where('create_time','<',$endYesterday)->count();
        $nanOldCount = DB::table('members')->where('sex',1)->where('create_time','>',$beginYesterday)->where('create_time','<',$endYesterday)->count();
        $nvOldCount = DB::table('members')->where('sex',2)->where('create_time','>',$beginYesterday)->where('create_time','<',$endYesterday)->count();
        $info = array(
            'todayCount'=>$todayCount,
            'nanTodayCount'=>$nanTodayCount,
            'nvTodayCount'=>$nvTodayCount,
            'yesterdayCount'=>$yesterdayCount,
            'nanOldCount'=>$nanOldCount,
            'nvOldCount'=>$nvOldCount
        );
        return view('admin/index')->with('login_log',$login_log)->with('info',$info);
    }

    public function project(){
        if(isset($_GET['page']) && $_GET['page']>0){
            $cpage = $_GET['page'];
        }else{
            $cpage = 1;
        }

        if(!empty($_GET['class'])){
            $total = DB::table('project')->where('class',$_GET['class'])->get()->count();
            $num = 8;
            $pagenum=ceil($total/$num);//总页数
            $offset=($cpage-1)*$num;//开始去数据的位置
            $projects = DB::table('project')->where('class',$_GET['class'])->orderBy('class','asc')->skip($offset)->take($num)->get()->map(function($v){return (array)$v;})->toArray();

        }else{
            $total = DB::table('project')->get()->count();
            $num = 8;
            $pagenum=ceil($total/$num);//总页数
            $offset=($cpage-1)*$num;//开始去数据的位置
            $projects = DB::table('project')->orderBy('class','asc')->skip($offset)->take($num)->get()->map(function($v){return (array)$v;})->toArray();

        }
//        $pager = pagination($total, $pindex, $psize);
        $start=$offset+1;//开始记录页
        $end=($cpage==$pagenum)?$total : ($cpage*$num);//结束记录页
        $next=($cpage==$pagenum)? $pagenum:($cpage+1);//下一页
        $prev=($cpage==1)? 1:($cpage-1);//前一页
        return view('admin/project')->with('projects',$projects)->with('total',$total)->with('prev',$prev)->with('next',$next)->with('pagenum',$pagenum)->with('cpage',$cpage);
    }

    public function del_project(){
        $id = $_GET['id'];
        $bool = DB::table('project')->where('id',$id)->delete();
        if($bool){
            echo "<script>alert('删除成功！');window.location.href = 'project.html';</script>";
            exit;
        }
    }

    public function edit_project(Request $request){
        if($request->isMethod('post')){
            $id = isset($_POST['id'])?$_POST['id']:'';
            if(!empty($id)){
                //修改
                $data = array(
                    'project'=>$_POST['project'],
                    'class'=>$_POST['class']
                );
                $bool = DB::table('project')->where('id',$id)->update($data);
                if($bool){
                    echo "<script>alert('修改成功！');window.location.href = 'project.html';</script>";
                    exit;
                }else{
                    echo "<script>alert('修改失败！');window.location.href = 'edit_project.html?id='".$id.";</script>";
                    exit;
                }
            }else{
                //添加
                $data = array(
                    'project'=>$_POST['project'],
                    'class'=>$_POST['class']
                );
                $bool = DB::table('project')->insert($data);
                if($bool){
                    echo "<script>alert('添加成功！');window.location.href = 'project.html';</script>";
                    exit;
                }else{
                    echo "<script>alert('添加失败！');window.location.href = 'edit_project.html';</script>";
                    exit;
                }
            }
        }else{
            $id = isset($_GET['id'])?$_GET['id']:'';
            if(!empty($id)){
                //修改
                $log = (array)DB::table('project')->where('id',$id)->first();
                return view('admin/edit_project',compact('log',$log));
            }else{
                //添加
                return view('admin/edit_project');
            }
        }
    }
//---------------------------------------------
    public function user(){
        $where = ' where 1 = 1 ';
        $nickname ='';
        $urlarr = array();
        if(isset($_GET['nickname']) &&$_GET['nickname'] !== ''){
            $where .= " and (id='".$_GET['nickname']."' or nickname = '".$_GET['nickname']."') ";
            $nickname = $_GET['nickname'];
            $urlarr[]="nickname=".$_GET['nickname'];
        }
        $sex = '';
        if(isset($_GET['sex']) &&$_GET['sex'] !==''){
            $where .= " and sex = ".$_GET['sex'];
            $sex = $_GET['sex'];
            $urlarr[]="sex=".$_GET['sex'];
        }
        $status = '';
        if(isset($_GET['status']) && $_GET['status'] !== ''){
            $where .= " and status = ".$_GET['status'];
            $status = $_GET['status'];
            $urlarr[]="status=".$_GET['status'];
        }
        $vip = '';
        if(isset($_GET['vip']) &&$_GET['vip'] !== ''){
            //未开通
            if($_GET['vip'] == 0){
                $where .= " and (vip < ".time().' or vip is null) ';
            }else if($_GET['vip'] == 1){
                $where .= " and vip > ".time();
            }
            $vip = $_GET['vip'];
            $urlarr[]="vip=".$_GET['vip'];
        }
        $is_feng = '';
        if(isset($_GET['is_feng']) &&$_GET['is_feng'] !== ''){
            $where .= " and is_feng = ".$_GET['is_feng'];
            $is_feng = $_GET['is_feng'];
            $urlarr[]="is_feng=".$_GET['is_feng'];
        }
        $url = '';
        if($where !=' where 1 = 1 '){
            $url='&'.implode('&',$urlarr);
        }
        $sql = 'select * from ims_members '.$where.' order by create_time desc ';
        $logs = DB::select($sql);
        if(!empty($logs)){
            foreach($logs as $k=>$v){
                $a = (array)$v;
                $logs[$k] = $a;
            }
        }
        $total = count($logs);
        if(isset($_GET['page']) && $_GET['page']>0){
            $cpage = $_GET['page'];
        }else{
            $cpage = 1;
        }
        $num = 8;
        $pagenum=ceil($total/$num);//总页数
        $offset=($cpage-1)*$num;//开始去数据的位置
        $sql = 'select * from ims_members '.$where.' order by create_time desc limit '.$offset.','.$num;
//            var_dump($sql);die;
        $logs = DB::select($sql);
        if(!empty($logs)){
            foreach($logs as $k=>$v){
                $a = (array)$v;
                $logs[$k] = $a;
            }
        }
        $start=$offset+1;//开始记录页
        $end=($cpage==$pagenum)?$total : ($cpage*$num);//结束记录页
        $next=($cpage==$pagenum)? $pagenum:($cpage+1);//下一页
        $prev=($cpage==1)? 1:($cpage-1);//前一页
        return view('admin/user')->with('logs',$logs)
            ->with('total',$total)->with('prev',$prev)->with('next',$next)->with('pagenum',$pagenum)->with('cpage',$cpage)
            ->with('nickname',$nickname)->with('sex',$sex)->with('status',$status)->with('vip',$vip)->with('url',$url)
            ->with('is_feng',$is_feng);
    }

    public function editTop(){
        $top = $_GET['top'];
        $id = $_GET['id'];
        $bool = DB::table('members')->where('id',$id)->update(array('top'=>$top));
        if($bool){
            echo "<script>alert('修改成功！');window.location.href = 'user.html';</script>";
            exit;
        }else{
            echo "<script>alert('修改失败！');window.location.href = 'user.html';</script>";
            exit;
        }
    }

    //5.消费收入明细
    public function consum(){
        $urlarr = array();
        $nickname ='';
        $where ='';
        if(isset($_GET['nickname']) && !empty($_GET['nickname'])){
            $where .= " and (t2.id='".$_GET['nickname']."' or t2.nickname = '".$_GET['nickname']."') ";
            $nickname = $_GET['nickname'];
            $urlarr[]="nickname=".$_GET['nickname'];
        }
        $time ='';
        if(isset($_GET['time']) && !empty($_GET['time'])){
            $arr = explode('~',$_GET['time']);
            $time1 = strtotime($arr[0]);
            $time2 = strtotime($arr[1]);
            $where .= " and t1.create_time >= $time1 and t1.create_time <= $time2 ";
            $time = $_GET['time'];
            $urlarr[]="time=".$_GET['time'];
        }
        $url = '';
        if($where != ''){
            $url='&'.implode('&',$urlarr);
        }
//        var_dump($where);die;
        $sql = " select t1.id  from ims_paylog as t1 left join ims_members as t2 on t1.uid =t2.id left join ims_members as t3 on t1.fid = t3.id where t1.class = 2".$where;
        $logs = DB::select($sql);
        if(!empty($logs)){
            foreach($logs as $k=>$v){
                $a = (array)$v;
                $logs[$k] = $a;
            }
        }
//        var_dump($logs);die;
        $total = count($logs);
        if(isset($_GET['page']) && $_GET['page']>0){
            $cpage = $_GET['page'];
        }else{
            $cpage = 1;
        }
        $num = 3;
        $pagenum=ceil($total/$num);//总页数
        $offset=($cpage-1)*$num;//开始去数据的位置
        $sql = " select t2.id as uid,t3.id as fid,t2.nickname as getname,t3.nickname as payname,t1.class,t1.price ,t1.create_time,t1.id  from ims_paylog as t1 left join ims_members as t2 on t1.uid =t2.id left join ims_members as t3 on t1.fid = t3.id where t1.class = 2".$where." limit $offset,$num";
        $logs = DB::select($sql);
        if(!empty($logs)){
            foreach($logs as $k=>$v){
                $a = (array)$v;
                $logs[$k] = $a;
            }
        }
        $start=$offset+1;//开始记录页
        $end=($cpage==$pagenum)?$total : ($cpage*$num);//结束记录页
        $next=($cpage==$pagenum)? $pagenum:($cpage+1);//下一页
        $prev=($cpage==1)? 1:($cpage-1);//前一页
        return view('admin/consum')->with('logs',$logs)->with('nickname',$nickname)->with('total',$total)->with('prev',$prev)->with('next',$next)->with('pagenum',$pagenum)->with('cpage',$cpage)
            ->with('nickname',$nickname)->with('url',$url)->with('time',$time);
    }

    public function recharge(){
        return view('admin/recharge');
    }

    public function top(Request $request){
        if($request->isMethod('post')){
            $id = $_POST['id'];
            if(!empty($_POST['num'])){
                $num = $_POST['num'];
                $now = $_POST['now'];
                $arr = array('now'=>$now,'count'=>$num);
                $bool =  DB::table('members')->where('id',$id)->update(array('top_num'=>json_encode($arr),'top'=>null));
                if($bool){
                    echo "<script>alert('修改成功！');window.location.href = 'user.html';</script>";
                    exit;
                }else{
                    echo "<script>alert('修改失败！');window.location.href = 'user.html';</script>";
                    exit;
                }
            }
            if(!empty($_POST['top'])){
                $time = strtotime($_POST['top']);
                $bool2 =  DB::table('members')->where('id',$id)->update(array('top'=>$time,'top_num'=>null));
                if($bool2){
                    echo "<script>alert('修改成功！');window.location.href = 'user.html';</script>";
                    exit;
                }else{
                    echo "<script>alert('修改失败！');window.location.href = 'user.html';</script>";
                    exit;
                }
            }
        }else{
            $id = $_GET['id'];
            $info = (array)DB::table("members")->select('id','top','top_num')->where('id',$id)->first();
            if(!empty($info)){
            	$arr = json_decode($info['top_num'],true);
            	$now = $arr['now'];
            	$count = $arr['count'];
            	$logs = array('id'=>$id,'top'=>$info['top'],'count'=>$count,'now'=>$now);
            }else{
            	$logs = array('id'=>$id,'top'=>'','count'=>'','now'=>'');
            }
            return view("admin/top")->with('logs',$logs);
        }

    }
}
